package com.antdesignbackend.backend.harmlessTreatment.controller;

import java.io.Serializable;
import java.util.List;

import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import com.antdesignbackend.backend.common.web.BaseController;
import com.antdesignbackend.backend.common.returnResult.ReturnVO;
import com.antdesignbackend.backend.aop.AuthToken;
import com.antdesignbackend.backend.harmlessTreatment.entity.HarmlessTreatment;
import com.antdesignbackend.backend.harmlessTreatment.entity.entityVo.HarmlessTreatmentVo;
import com.antdesignbackend.backend.harmlessTreatment.service.harmlessTreatmentService;

/**
 * 无害化处理途径设置
 *
 * @author 林宇杰
 * @email harmlesstreatment
 * @date 2020-09-21 16:43:00
 */

@Controller
@RequestMapping("/harmlessTreatment")
@Slf4j
public class HarmlessTreatmentController extends BaseController{

	private static final String FUN_NAME = "无害化处理途径设置";//功能名称

	@Autowired
	private harmlessTreatmentService harmlessTreatmentService;

	@GetMapping("/index")
	@AuthToken
	public String index() {
		return "harmlessTreatment/index";
	}


	@PostMapping("/tableSearch")
	@ResponseBody
	ReturnVO tableSearch(@RequestBody HarmlessTreatmentVo harmlessTreatmentVo) throws SecurityException, IllegalArgumentException{
		ReturnVO result = new ReturnVO();
		QueryWrapper<HarmlessTreatment> wrapper = new QueryWrapper<>();
		Page<HarmlessTreatment> page = new Page<>(harmlessTreatmentVo.getPageIndex(), harmlessTreatmentVo.getPageSize());
		IPage<HarmlessTreatment> pageResult = harmlessTreatmentService.page(page, wrapper);
		result.setTotal((int)pageResult.getTotal());
		result.setData(pageResult.getRecords());
		return result;

	}
	@PostMapping("/getHarmlessTreatment")
	@ResponseBody
	ReturnVO getHarmlessTreatment() throws SecurityException, IllegalArgumentException{
		ReturnVO result = new ReturnVO();
		QueryWrapper<HarmlessTreatment> wrapper = new QueryWrapper<>();
		wrapper.eq("enable",1);
		List<HarmlessTreatment> pageResult = harmlessTreatmentService.list(wrapper);
		result.setData(pageResult);
		return result;
	}
	@GetMapping("/getById/{id}")
	@ResponseBody
	public HarmlessTreatment getById(@PathVariable(name = "id") String id) {
		HarmlessTreatment obj = harmlessTreatmentService.getById(id);
		return obj;
	}

	@PostMapping("/save")
	@ResponseBody
	public ReturnVO save(@RequestBody HarmlessTreatment harmlessTreatment){
		ReturnVO result = new ReturnVO();
		if(harmlessTreatmentService.save(harmlessTreatment)){
			result.setMessage("保存成功！");
			result.setCode("200");
		}else{
			result.setMessage("保存失败！");
			result.setCode("500");
		}
		return result;
	}

	@PostMapping("update")
	@ResponseBody
	public ReturnVO updateById(@RequestBody HarmlessTreatment harmlessTreatment){
		ReturnVO result = new ReturnVO();
		HarmlessTreatment harmlessTreatment1=harmlessTreatmentService.getById(harmlessTreatment.getId());
		if(harmlessTreatment1.getEnable()==0){
			harmlessTreatment.setEnable(1);
		}
		if(harmlessTreatment1.getEnable()==1){
			harmlessTreatment.setEnable(0);
		}
		if(harmlessTreatmentService.updateById(harmlessTreatment)){
			result.setMessage("保存成功！");
			result.setCode("200");
		}else{
			result.setMessage("保存失败！");
			result.setCode("500");
		}
		return result;
	}

	/**
     * 根据id删除
     */
	@DeleteMapping("/deleteById/{id}")
	@ResponseBody
	public ReturnVO deleteById(@PathVariable Serializable id) {
		ReturnVO result = new ReturnVO();
		if (harmlessTreatmentService.removeById(Integer.parseInt(id.toString())) == true) {
			result.setMessage("删除成功");
		} else {
			result.setMessage("删除失败");
			result.setCode("0");
		}
		return result;
	}

}
